# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import scrapy
from scrapy.pipelines.images import ImagesPipeline

# class DownloadimgPipeline:
#     def process_item(self, item, spider):
#         return item

class DownloadimgPipeline(ImagesPipeline):
    """
    继承ImagesPipeline 必须重写以下方法
    """
    def get_media_requests(self, item, info):
        """
        发送请求
        """
        print("开始下载" + item['img_url'])
        yield scrapy.Request(item['img_url'])
    
    def file_path(self, request, response=None, info=None):
        """图片路径"""
        img_name = request.url.split('/')[-1]
        return img_name
    
    def item_completed(self, results, item, info):
        # 返回给下一个管道使用
        print(item['img_url'], '下载完成！')
        return item
